/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prueba.binder;

import java.util.Map;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.NotFoundException;

/**
 *
 * @author HP_Propietario
 */
public class ServletBinder implements Binder {

    public ServletBinder() {
    }

    public Object loadObject(Class clazz, Map map) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {

        //1.buscamos si existe la clase especificada.
        //2.creamos una instancia del objecto.
        //3.contruimos el map. name-value.
        //4.buscamos la propiedades definidas.




        ClassPool pool = new ClassPool();
        pool.appendSystemPath();
        CtClass ctClass = pool.getCtClass(clazz.getName());
        Object object = ctClass.toClass().newInstance();

        return new Object();

    }

    public CtField findProperty(String propertyName, CtClass ctClass) throws NotFoundException {
        CtField ctField = ctClass.getDeclaredField(propertyName);
        if (ctField == null) {
            //no podemos seguir
        }
        return ctField;
    }

    public CtMethod findMethod(String methodName, CtClass ctClass) throws NotFoundException {
        return ctClass.getDeclaredMethod(methodName);


    }

    public void setProperty(String propertyName, CtClass ctClass, Object[] properties) throws NotFoundException {
        CtField ctField = findProperty(propertyName, ctClass);

    }

    public boolean validateBean(String field, CtClass ctClass) throws NotFoundException {
        boolean isValido = false;
        findProperty(field, ctClass);


        //si el campo es nombre entoces:
        //ombre
        String ombre = field.substring(1, field.length());
        //-->N
        String N = field.substring(0, 1).toUpperCase();
        //-->getNombre
        String pat = "get" + N + ombre;
        //-->setNombre
        String patSet = "set" + N + ombre;
        findMethod(pat, ctClass);
        findMethod(patSet, ctClass);
        


        return isValido;

    }
}
